package models

import "github.com/astaxie/beego/orm"

type MenuModel struct {
	Id     int `orm:pk;auto`
	Parent int
	Name   string `orm:"size(45)"`
	Seq    int
	Format string `orm:"size(2048);default({})"`
}

type MenuTree struct {
	MenuModel
	Child []MenuModel
}

func (m *MenuModel) TableName() string {
	return "cms_menu"
}

func MenuStruct() map[int]MenuTree {
	query := orm.NewOrm().QueryTable(new(MenuModel).TableName())
	data := make([]*MenuModel, 0)
	query.OrderBy("parent", "-seq").All(&data)

	var menu = make(map[int]MenuTree)
	if len(data)>0{
		for _,v:=range data{
			if 0 == v.Parent{
				var tree = new(MenuTree)
				tree.MenuModel=*v
				menu[v.Id] = *tree
			}else{
				if tmp,ok:=menu[v.Parent];ok{
					tmp.Child = append(tmp.Child,*v)
					menu[v.Parent] = tmp
				}
			}
		}
	}
	return menu
}

func MenuList() ([]*MenuModel, int64) {
	query := orm.NewOrm().QueryTable(new(MenuModel).TableName())
	total,_ := query.Count()
	data := make([]*MenuModel,0)
	query.OrderBy("parent","-seq").All(&data)
	return data,total
}
